﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;
using LiuYan.Web.Controllers;

namespace LiuYan.Web.Areas.Mall.Controllers
{
    public class CenterController : BaseController
    {
        //
        // GET: /Mall/Center/

        public ActionResult Address()
        {
            ViewData["data"] = new AddressService().GetAddress(UserInfo().Id);
            ViewData["Province"] = new AddressService().GetProvince();
            return View();
        }
        public ActionResult ShopCar()
        {
            ViewData["shopcarlist"] = new ShopCarService().GetList(UserInfo().Id);
            return View();
        }
       
        public ActionResult User()
        {
            ViewData["img"] = UserInfo().Img;
            ViewData["name"] = UserInfo().NcName;
            ViewData["shopcarcount"] = new ShopCarService().GetShopCarCount(UserInfo().Id);
            ViewData["ordercount"] = new OrderService().GetOrderCount(UserInfo().Id);
            return View();
        }

        public ActionResult PingJia()
        {
            return View();
        }

        public string Pjlist(int pageIndex, int pageCount)
        {
            return JsonConvert.SerializeObject(new EvaluateService().GetUList(pageIndex, pageCount, UserInfo().Id));
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public string UploadImg()
        {
            HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
            int num = file.Count;
            string imgPath = "";
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string fileName = file[0].FileName.Split('\\').Last();
            imgPath = "/Content/goodimg/" + Convert.ToInt64(ts.TotalSeconds) + "." + fileName.Split('.').Last();
            string path = Server.MapPath(imgPath);
            file[0].SaveAs(path);
            return "{'status':'1','path':'" + imgPath + "'}";
        }
        public ActionResult UserDes()
        {
            var row = new UserService().GetUserInfo(UserInfo().Id).Rows[0];
            ViewData["img"] = row["Img"];
            ViewData["ncname"] = row["NcName"];
            ViewData["name"] = row["Name"];
            ViewData["tell"] = row["Tell"];
            ViewData["sex"] = row["Sex"];
            return View();
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int AddOrder(string ids)
        {
          return  new OrderService().AddOrder(ids,UserInfo().Id);
        }
        /// <summary>
        /// 修改购物车数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int EditShopCarCount(int id, int count)
        {
            return new ShopCarService().EditCount(id, count);
        }

        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public int EditAddress(FormCollection form)
        {
            TB_Address entity=new TB_Address();
            entity.Id = 0;
            entity.Default = Convert.ToInt32(form["defalut"]);
            entity.Address = form["address"];
            entity.Tell = form["tell"];
            entity.Name = form["name"];
            entity.UserId = UserInfo().Id;
           return new AddressService().Edit(entity);
        }

        /// <summary>
        /// 设为默认地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int IsDefault(int id)
        {
            return new AddressService().IsDefault(id,UserInfo().Id);
        }
        /// <summary>
        /// 获取城市
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCity(int id)
        {
            return JsonConvert.SerializeObject(new AddressService().GetCity(id));
        }
        /// <summary>
        /// 获取区县
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetDistrict(int id)
        {
            return JsonConvert.SerializeObject(new AddressService().GetDistrict(id));
        }

        /// <summary>
        /// 获取地址
        /// </summary>
        /// <returns></returns>
        public string GetAddress()
        {
            return JsonConvert.SerializeObject(new AddressService().GetAddress(UserInfo().Id));
        }

        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelAddress(int id)
        {
            return new BaseService().Dels(id.ToString(),"tb_address",false);
        }
        /// <summary>
        /// 删除购物车数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelShopCar(string id)
        {
            return new BaseService().Dels(id, "tb_shopcar", true);
        }

        public int EditUserInfo(FormCollection form)
        {
            TB_User entity=new TB_User();
            entity.Id = UserInfo().Id;
            entity.Img = form["img"];
            entity.Name = form["name"];
            entity.NcName = form["ncname"];
            entity.Sex = Convert.ToInt32(form["sex"]);
            entity.Tell = form["tell"];
            entity.OpenId = "";
            return new UserService().Edit(entity);
        }

       
    }
}
